package com.yili.oa.test;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yili.oa.models.*;
import com.yili.oa.service.*;
import com.yili.oa.utils.Exception.AjaxException;
import com.yili.oa.utils.ListUtils;
import com.yili.oa.vo.request.UserExchangeReq;
import com.yili.oa.vo.request.UserRechargeReq;
import com.yili.oa.vo.response.*;
import com.yili.oa.vo.special.FtBetRecordSpec;
import com.yili.oa.vo.special.RewardHistorySpec;
import io.swagger.annotations.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.math.BigDecimal;
import java.util.*;

/**
 * @author lxr
 * @version v0.01
 * @date 2019/8/17 0017
 * @email 200890949@qq.com
 */
@Api("游戏接口2")
@RestController
@RequestMapping("/tempApi")
public class Temp2Controller {

    @Autowired
    private FtVipService ftVipService;

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private TurnoverService turnoverService;

    @Autowired
    private FtBetRecordService  ftBetRecordService;

    @Autowired
    private UserService userService;

    @Autowired
    private GameService gameService;



    @GetMapping("/getVip/{lv}")
    @ApiOperation(value = "得到当前用户的vip等级")
    public FtVipRes getVip(@ApiParam(value = "用户等级", type = "Integer", required = true) @PathVariable("lv")Integer lv){
        FtVipRes ftVipRes = ftVipService.getByLv(lv);
        if (ftVipRes==null){
            throw new AjaxException("不存在此vip等级");
        }
        return ftVipRes;
    }

    @GetMapping("/getNotice/{type}")
    @ApiOperation(value = "type:0浮动公告 1日常消息 2游戏帮助")
    public List<NoticeRes> getNotice(@ApiParam(value = "用户等级", type = "Integer", required = true) @PathVariable("type")Integer type){
        List<NoticeRes> list = noticeService.getByType(type);
        if (list==null){
            throw new AjaxException("不存在此消息列表");
        }
        return noticeService.getByType(type);
    }

    @PostMapping("/addExchange")
    @ApiOperation(value = "添加提现记录")
    public UserExchangeRes addUserExchange(@ApiParam("提现记录") UserExchangeReq userExchange){
        if (StringUtils.isBlank(userExchange.getUserId())){
            throw new AjaxException("请输入用户id");
        }
        if (StringUtils.isBlank(userExchange.getBankAccount())){
            throw new AjaxException("请输入银行账号");
        }
        if (StringUtils.isBlank(userExchange.getBank())){
            throw new AjaxException("请输入银行账号");
        }
        if (StringUtils.isBlank(userExchange.getBankUser())){
            throw new AjaxException("请输入持卡人");
        }
        if (userExchange.getExchangeNum()==null || userExchange.getExchangeNum()==0){
            throw new AjaxException("请输入提现金额");
        }
        if (userExchange.getTime()==null || userExchange.getTime()==0){
            throw new AjaxException("请输入时间");
        }
        UserExchange result = turnoverService.addExchange(userExchange);
        if (result==null){
            return null;
        }
        UserExchangeRes userExchangeRes = new UserExchangeRes();
        BeanUtils.copyProperties(result,userExchangeRes);
        return userExchangeRes;
    }

    @GetMapping("/getExchange/{userId}")
    @ApiOperation(value = "获得用户的提现记录")
    @ApiImplicitParams({@ApiImplicitParam(name="userId",value="用户id",required=true,paramType = "query"),
            @ApiImplicitParam(name="page",value="第几页",required=true,paramType = "query"),
            @ApiImplicitParam(name="limit",value="每页显示数量",required=true,paramType = "query")})
    public List<UserExchangeRes> getExchange(@ApiIgnore String userId,@ApiIgnore Integer page,@ApiIgnore Integer limit){
        PageHelper.startPage(page, limit);
        List<UserExchange> list = turnoverService.getList(null,userId);
        PageInfo pageInfo = new PageInfo<>(list,limit);
        List<UserExchangeRes> result = new ArrayList<>();
        for (UserExchange userExchange:
             list) {
            UserExchangeRes userExchangeRes = new UserExchangeRes();
            BeanUtils.copyProperties(userExchange,userExchangeRes);
            result.add(userExchangeRes);
        }
        return result;
    }

    @PostMapping("/addRecharge")
    @ApiOperation(value = "添加充值记录")
    public UserRechargeRes addUserRecharge(@ApiParam("充值记录") UserRechargeReq userRechargeReq){
        if (StringUtils.isBlank(userRechargeReq.getUserId())){
            throw new AjaxException("请输入用户id");
        }
        if (StringUtils.isBlank(userRechargeReq.getRechargeChannel())){
            throw new AjaxException("请输入充值渠道");
        }
        if (StringUtils.isBlank(userRechargeReq.getRechargeType())){
            throw new AjaxException("请输入充值方式");
        }
        if (userRechargeReq.getRechargeNum()==null || userRechargeReq.getRechargeNum()==0){
            throw new AjaxException("请输入充值数量");
        }
        if (userRechargeReq.getTime()==null || userRechargeReq.getTime()==0){
            throw new AjaxException("请输入时间");
        }
        UserRecharge result = turnoverService.addRecharge(userRechargeReq);
        if (result==null){
            return null;
        }
        UserRechargeRes userRechargeRes = new UserRechargeRes();
        BeanUtils.copyProperties(result,userRechargeRes);
        return userRechargeRes;
    }

    @GetMapping("/getRecharge/{userId}")
    @ApiOperation(value = "获得用户的充值记录")
    @ApiImplicitParams({@ApiImplicitParam(name="userId",value="用户id",required=true,paramType = "query"),
            @ApiImplicitParam(name="page",value="第几页",required=true,paramType = "query"),
            @ApiImplicitParam(name="limit",value="每页显示数量",required=true,paramType = "query")})
    public List<UserRechargeRes> getRecharge(@ApiIgnore String userId,@ApiIgnore Integer page,@ApiIgnore Integer limit){
        PageHelper.startPage(page, limit);
        List<UserRecharge> list = turnoverService.getRechargeList(null,userId);
        List<UserRechargeRes> result = new ArrayList<>();
        for (UserRecharge userRecharge:
                list) {
            UserRechargeRes userRechargeRes = new UserRechargeRes();
            BeanUtils.copyProperties(userRecharge,userRechargeRes);
            result.add(userRechargeRes);
        }
        return result;
    }

    @Cacheable(value="reward",key="'reward_'+#userId")
    @GetMapping("/getReward")
    @ApiOperation(value = "获得用户的洗码列表(不需要传时间)/获得用户洗码历史详情(要传date)")
    @ApiImplicitParams({ @ApiImplicitParam(name = "gamekindId", value = "游戏分类id", required = true, paramType = "query")
                ,@ApiImplicitParam(name = "userId", value = "用户id", required = true, paramType = "query")
                ,@ApiImplicitParam(name = "datetime", value = "洗码时间", paramType = "query")})
    public List<RewardRes> getReward(@ApiIgnore Long gamekindId
            ,@ApiIgnore Long userId
            ,@ApiIgnore Date datetime){
        //查找投注总额
        Float betSum = ftBetRecordService.getBetSum(userId);

        //查找平台
        List<FtGamePlatform> platform = gameService.getPlatform(gamekindId);
        //查找游戏
        List<AllGame> games = gameService.getGame(gamekindId);

        List<FtBetRecordSpec> list = new ArrayList<>();
        if (ListUtils.isNotNullOrEmpty(platform)) {
            //查找总投注记录
            if (datetime==null) {
                list = ftBetRecordService.getByUserId(userId, gamekindId, null);
            }else {
                list = ftBetRecordService.getByUserId(userId, gamekindId, datetime);
            }
        }else {
            if (datetime==null) {
                list = ftBetRecordService.getByGameId(userId, gamekindId, null);
            }else {
                list = ftBetRecordService.getByGameId(userId, gamekindId, datetime);
            }
        }

        List<RewardRes> result = new ArrayList<>();

        User user = userService.getByAccount(userId.intValue());
        Short lv = user.getLv();
        FtVipRes vip = ftVipService.getByLv(Integer.valueOf(lv));
        //假如有平台显示平台 没平台显示游戏
        if (ListUtils.isNotNullOrEmpty(platform)) {
            for (FtGamePlatform ftGamePlatform :
                    platform) {
                RewardRes rewardRes = new RewardRes();
                Long platFormId = ftGamePlatform.getId();
                String platFormName = ftGamePlatform.getGamePlatform();
                for (FtBetRecordSpec ftBetRecordSpec :
                        list) {
                    if (ftBetRecordSpec.getGamePlatform().equals(platFormId)) {
                        rewardRes.setPlayMoney(ftBetRecordSpec.getBetAmount().floatValue());
                        rewardRes.setRewardMoney((ftBetRecordSpec.getBetAmount().multiply(BigDecimal.valueOf(vip.getReward()).divide(new BigDecimal(100)))).floatValue());
                    } else {
                        rewardRes.setPlayMoney(0F);
                        rewardRes.setRewardMoney(0F);
                    }
                }
                rewardRes.setReward(vip.getReward());
                rewardRes.setGamePlatFormName(platFormName);
                rewardRes.setAllBet(betSum);
                result.add(rewardRes);
            }
        }else {
            for (AllGame game :
                    games) {
                RewardRes rewardRes = new RewardRes();
                Long gameId = game.getGameId();
                String gameName = game.getGameName();
                for (FtBetRecordSpec ftBetRecordSpec :
                        list) {
                    if (ftBetRecordSpec.getGameId().equals(gameId)) {
                        rewardRes.setPlayMoney(ftBetRecordSpec.getBetAmount().floatValue());
                        rewardRes.setRewardMoney((ftBetRecordSpec.getBetAmount().multiply(BigDecimal.valueOf(vip.getReward()).divide(new BigDecimal(100)))).floatValue());
                    } else {
                        rewardRes.setPlayMoney(0F);
                        rewardRes.setRewardMoney(0F);
                    }
                }
                rewardRes.setReward(vip.getReward());
                rewardRes.setGamePlatFormName(gameName);
                rewardRes.setAllBet(betSum);
                result.add(rewardRes);
            }
        }
        return result;
    }

    @CacheEvict(value="rewardHis",key="'rewardHis_'+#userId")
    @PostMapping("/reward")
    @ApiOperation(value = "洗码")
    public Map<String,String> reward(@ApiParam(name = "userId", value = "用户id", required = true) @RequestParam( value = "userId") Long userId){
        //查找投注总额
        Float betSum = ftBetRecordService.getBetSum(userId);
        //获得vip等级 根据vip等级获得洗码比例
        User user = userService.getByAccount(userId.intValue());
        Short lv = user.getLv();
        FtVipRes vip = ftVipService.getByLv(Integer.valueOf(lv));
        //洗码比例
        Float rewardPercent = vip.getReward();
        //查洗码
        Float money = new BigDecimal(betSum).multiply(new BigDecimal(rewardPercent).divide(new BigDecimal(100))).floatValue();
        if(money<=1.00F){
            throw new AjaxException("没有可领取的洗码金额(洗码金额需要大于1)");
        }
        List<FtBetRecord> list = ftBetRecordService.getByUserId(userId);
        Date date = new Date();
        BigDecimal allBetAmout = new BigDecimal(0);
        for (FtBetRecord ftBetRecord:
             list) {
            ftBetRecord.setXimaTime(date);
            allBetAmout.add(ftBetRecord.getBetAmount());
        }
        RewardHistorySpec rewardHistorySpec = new RewardHistorySpec();
        rewardHistorySpec.setRewardTime(date);
        rewardHistorySpec.setRewardSum(betSum);
        rewardHistorySpec.setRewardMoney(allBetAmout.multiply(new BigDecimal(rewardPercent)).floatValue());
        ftBetRecordService.add(rewardHistorySpec);

        userService.addCoins(userId.intValue(),allBetAmout.multiply(new BigDecimal(rewardPercent)).longValue());

        Map<String,String> map = new HashMap<>();
        map.put("200","success");
        return map;
    }

    @Cacheable(value="rewardHis",key="'rewardHis_'+#userId")
    @GetMapping("/getRewardHis")
    @ApiOperation(value = "获得用户的历史洗码记录")
    @ApiImplicitParams({@ApiImplicitParam(name="userId",value="用户id",required=true,paramType = "query"),
            @ApiImplicitParam(name="page",value="第几页",required=true,paramType = "query"),
            @ApiImplicitParam(name="limit",value="每页显示数量",required=true,paramType = "query")})
    public List<RewardHistorySpec> getRewardHis(@ApiIgnore String userId,@ApiIgnore Integer page,@ApiIgnore Integer limit){
        PageHelper.startPage(page, limit);
        List<RewardHistorySpec> list = ftBetRecordService.getRewarHist(Long.valueOf(userId));
        return list;
    }

/*    @GetMapping("/getRewardHisDesc")
    @ApiOperation(value = "获得用户的历史洗码记录详情")
    @ApiImplicitParams({@ApiImplicitParam(name = " gamekindId", value = "游戏分类id", required = true, paramType = "query")
            ,@ApiImplicitParam(name="datetime",value="时间",required=true,paramType = "query")
    ,@ApiImplicitParam(name="userId",value="用户id",required=true,paramType = "query")})
    public List<RewardRes> getRewardHis(@ApiIgnore Long  gamekindId,@ApiIgnore Date datetime,@ApiIgnore String userId){
        //查找平台
        List<FtGamePlatform> platform = gameService.getPlatform(gamekindId);
        //查找游戏
        List<AllGame> games = gameService.getGame(gamekindId);

        List<FtBetRecordSpec> list = new ArrayList<>();
        if (ListUtils.isNotNullOrEmpty(platform)) {
            //查找总投注记录
            list = ftBetRecordService.getByUserId(Long.valueOf(userId), gamekindId,datetime);
        }else {
            list = ftBetRecordService.getByGameId(Long.valueOf(userId),gamekindId,datetime);
        }

        List<RewardRes> result = new ArrayList<>();

        User user = userService.getByAccount(userId.intValue());
        Short lv = user.getLv();
        FtVipRes vip = ftVipService.getByLv(Integer.valueOf(lv));
        //假如有平台显示平台 没平台显示游戏
        if (ListUtils.isNotNullOrEmpty(platform)) {
            for (FtGamePlatform ftGamePlatform :
                    platform) {
                RewardRes rewardRes = new RewardRes();
                Long platFormId = ftGamePlatform.getId();
                String platFormName = ftGamePlatform.getGamePlatform();
                for (FtBetRecordSpec ftBetRecordSpec :
                        list) {
                    if (ftBetRecordSpec.getGamePlatform().equals(platFormId)) {
                        rewardRes.setPlayMoney(ftBetRecordSpec.getBetAmount().floatValue());
                        rewardRes.setRewardMoney((ftBetRecordSpec.getBetAmount().multiply(BigDecimal.valueOf(vip.getReward()).divide(new BigDecimal(100)))).floatValue());
                    } else {
                        rewardRes.setPlayMoney(0F);
                        rewardRes.setRewardMoney(0F);
                    }
                }
                rewardRes.setReward(vip.getReward());
                rewardRes.setGamePlatFormName(platFormName);
                rewardRes.setAllBet(betSum);
                result.add(rewardRes);
            }
        }else {
            for (AllGame game :
                    games) {
                RewardRes rewardRes = new RewardRes();
                Long gameId = game.getGameId();
                String gameName = game.getGameName();
                for (FtBetRecordSpec ftBetRecordSpec :
                        list) {
                    if (ftBetRecordSpec.getGameId().equals(gameId)) {
                        rewardRes.setPlayMoney(ftBetRecordSpec.getBetAmount().floatValue());
                        rewardRes.setRewardMoney((ftBetRecordSpec.getBetAmount().multiply(BigDecimal.valueOf(vip.getReward()).divide(new BigDecimal(100)))).floatValue());
                    } else {
                        rewardRes.setPlayMoney(0F);
                        rewardRes.setRewardMoney(0F);
                    }
                }
                rewardRes.setReward(vip.getReward());
                rewardRes.setGamePlatFormName(gameName);
                rewardRes.setAllBet(betSum);
                result.add(rewardRes);
            }
        }
        return result;
    }*/
}
